package  
{
	import org.flixel.*;
	/**
	 * ...
	 * @author ...
	 */
	 
	public class EndingState extends FlxState
	{
		private var _bestEnding:FlxSprite;
		private var _goodEnding:FlxSprite;
		private var _badEnding:FlxSprite;
		
		private static const _bronzeWeight:uint = 1;
		private static const _silverWeight:uint = 2;
		private static const _goldWeight:uint = 3;
		
		private static const _ending1MinScore:uint = 0;
		private static const _ending2MinScore:uint = 7;
		private static const _ending3MinScore:uint = 15;
		
		private static var _numberofBronzes:uint = 0;
		private static var _numberofSilvers:uint = 0;
		private static var _numberofGolds:uint = 0;
		
		private const _yBuffer:uint = 20;
		
		public static var currentEnding:uint = 0;
		private static var _beenToCredits:Boolean = false;
		
		private var _counter:Number = 0;
		private var _timeLimit:Number = 10;
		
		override public function create():void 
		{
			_bestEnding = new FlxSprite(0, 0, ImageFiles.endingBest);
			_bestEnding.exists = false;
			add(_bestEnding);
			
			_goodEnding = new FlxSprite(0, 0, ImageFiles.endingGood);
			_goodEnding.exists = false;
			add(_goodEnding);
			
			_badEnding = new FlxSprite(0, 0, ImageFiles.endingBad);
			_badEnding.exists = false;
			add(_badEnding);
			
			currentEnding = calculateEnding();
			
			FlxG.flash.start(0xFF000000);
		}
		
		override public function update():void 
		{
			//if (currentEnding == 0)
			{
				switch (currentEnding)
				{
					case PlayState.UNFINISHED: //called if not all levels are finished.
					_badEnding.exists = true;
					//FlxG.state = new LevelSelectState;
					break;
					case PlayState.BRONZE:
					_badEnding.exists = true;
					break;
					case PlayState.SILVER:
					_goodEnding.exists = true;
					break;
					case PlayState.GOLD:
					_bestEnding.exists = true;
					break;
				}
			}
			
			super.update();
			
			if (timeUp())
			{
				FlxG.fade.start(0xFF000000, 1, onFade);
			}
			
			//FlxG.fade.update();
			FlxG.flash.update();
		}
		
		private function timeUp():Boolean
		{
			_counter += FlxG.elapsed;
			
			if (_counter >= _timeLimit)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		private function onFade():void
		{
			if (!_beenToCredits)
			{
				_beenToCredits = true;
				FlxG.state = new CreditsState(false);
			}
			else
			{
				FlxG.state = new LevelSelectState();
			}
		}
		
		public static function isNextEndingUnlocked():Boolean
		{
			if (calculateEnding() > currentEnding && FlxG.scores[4] > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public static function calculateEnding():uint
		{
			_numberofBronzes = 0;
			_numberofSilvers = 0;
			_numberofGolds = 0;
			
			var tempScore:Number;
			
			for (var i:int = 0; i < 5; i++)
			{
				switch(PlayState.calculateRating(FlxG.scores[i], i))
				{
					case PlayState.UNFINISHED: //if any unfinished, function will return UNFINISHED
					return PlayState.UNFINISHED;
					break;
					case PlayState.BRONZE:
					_numberofBronzes++;
					break;
					case PlayState.SILVER:
					_numberofSilvers++;
					break;
					case PlayState.GOLD:
					_numberofGolds++;
					break;
				}
			}
			
			tempScore = _numberofBronzes * _bronzeWeight + _numberofGolds * _goldWeight + _numberofSilvers * _silverWeight;
			
			if (tempScore < _ending2MinScore)
			{
				return PlayState.BRONZE;
			}
			else if (tempScore < _ending3MinScore)
			{
				return PlayState.SILVER;
			}
			else
			{
				return PlayState.GOLD;
			}
		}
		
	}

}